<template>
  <div class="login">
    <van-nav-bar @click-left="$router.back()" class="login_head">
      <template #left>
        <span class="iconfont icon-left-arrow"></span>
      </template>
    </van-nav-bar>
    <h2>你好，请登录</h2>
    <van-form @submit="onSubmit">
      <van-field
        v-model="form.mobile"
        placeholder="请输入手机号"
        :rules="rules.mobile"
      >
        <template #left-icon>
          <span class="iconfont icon-iPhone lefticon"></span>
        </template>
      </van-field>
      <van-field
        v-model="form.code"
        placeholder="请输入验证码"
        :rules="rules.code"
      >
        <template #left-icon>
          <van-icon name="records" class="lefticon codeicon" />
        </template>
        <template #button>
          <van-button
            plain
            hairline
            type="default"
            class="iconbtn"
            @click="getCode"
            native-type="button"
            :disabled="totalTime !== 60"
            >{{
              totalTime === 60 ? '获取验证码' : `${totalTime}s后重试`
            }}</van-button
          >
        </template>
      </van-field>
      <div class="protocal">
        登录即同意<span>《用户使用协议》</span>和<span>《隐私协议》</span>
      </div>
      <van-button class="loginbtn" type="danger" round block>确定</van-button>
    </van-form>
  </div>
</template>

<script>
import { apiSendCode, apiLogin } from '@/api/login.js'
import { localSet } from '@/utils/mylocal.js'
export default {
  data () {
    return {
      totalTime: 60,
      form: {
        mobile: '18888881111',
        code: ''
      },
      rules: {
        mobile: [
          { required: true, message: '不能为空', trigger: 'onBlur' },
          {
            pattern: /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/,
            message: '手机号格式不正确',
            trigger: 'onBlur'
          }
        ],
        code: [
          { required: true, message: '不能为空', trigger: 'onBlur' },
          { pattern: /^\d{4}$/, message: '请输入4位', trigger: 'onBlur' }
        ]
      }
    }
  },
  methods: {
    //   获取验证码
    async getCode () {
      // 倒计时
      this.totalTime = 59

      const timer = setInterval(() => {
        this.totalTime--
        if (this.totalTime === 0) {
          clearInterval(timer)
          this.totalTime = 60
        }
      }, 1000)
      try {
        const res = await apiSendCode(this.form.mobile)
        this.totalTime = 60
        clearInterval(timer)
        this.$toast.success(res.data.data)
      } catch {
        this.$toast.fail('手机号格式不正确')
      }
    },
    async onSubmit () {
      try {
        const res = await apiLogin(this.form)
        if (res.data.code === 200) {
          this.$toast.success('登录成功')
          localSet('token', res.data.data.jwt)
          this.$router.push('/home')
        }
      } catch {
        this.$toast.fail('登录失败')
      }
    }
  }
}
</script>

<style lang="less" scoped>
.login {
  .login_head {
    span {
      font-size: 30px;
      color: #000;
    }
  }
  h2 {
    padding: 30px 0;
    margin-left: 16px;
  }
  .iconbtn {
    color: #01b8db;
  }
  .protocal {
    font-size: 14px;
    padding-top: 10px;
    padding-bottom: 50px;
    padding-left: 16px;
    span {
      color: #01b8db;
    }
  }
  .loginbtn {
    width: 90%;
    margin: 0 auto;
  }
  .lefticon {
    font-size: 20px;
  }
  .codeicon {
    margin-top: 10px;
  }
}
</style>
